/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command.listener.exe;

import cef.command.Command;
import cef.command.listener.exe.CommandExeEvent;
import cef.command.listener.exe.CommandExeListener;
import cef.command.listener.exe.FigureEditorExeComponent;

import coalescent.cef.command.RunBackwardRecursionCommand;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import java.util.List;

/**
 * @author Susanta Tewari
 */
public class VisualizeRecursionCacheDistn extends FigureEditorExeComponent {

    /** Field description */
    private static String rowKey = "CacheSize";

    /** Field description */
    private JFreeChart chart;

    /**
     * Method description
     *
     *
     * @param cmd
     */
    @Override
    public void initComponents(Command cmd) {

        title = "Recursion Cache Distribution";
        chart = ChartFactory.createLineChart(title, "Cut-Set", "CacheSize", null,
                PlotOrientation.VERTICAL, false, true, false);


        // change the range axis to integer units
        NumberAxis rangeAxis = (NumberAxis) chart.getCategoryPlot().getRangeAxis();

        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        figure = new ChartPanel(chart);
    }

    /**
     * Method description
     *
     *
     * @param event
     */
    @Override
    public void updateComponentData(CommandExeEvent event) {

        if (event.getSource() instanceof RunBackwardRecursionCommand) {

            RunBackwardRecursionCommand command = (RunBackwardRecursionCommand) event.getSource();


            // update data
            List<Integer> cacheSize        = command.getCurrentRecursionCacheSize();
            DefaultCategoryDataset dataset = new DefaultCategoryDataset();

            for (int i = 0; i < cacheSize.size(); i++) {

                dataset.setValue(cacheSize.get(i), rowKey, Integer.valueOf(i));
            }

            chart.getCategoryPlot().setDataset(dataset);
        }
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public Class<? extends Command> getTargetCommandClass() {
        return RunBackwardRecursionCommand.class;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public CommandExeListener newInstance() {
        return new VisualizeRecursionCacheDistn();
    }
}
